<?php
// +----------------------------------------------------------------------
// | OneKeyAdmin [ Believe that you can do better ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://onekeyadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: MUKE <513038996@qq.com>
// +----------------------------------------------------------------------
namespace plugins\pay\admin\controller;

use think\facade\View;
use think\facade\Filesystem;
use app\admin\BaseController;
use app\admin\model\Config as ConfigModel;
/**
 * 配置
 */
class Config extends BaseController
{
	public function alipay()
    {
    	$name = 'app_pay_alipay';
        if ($this->request->isPost()) {
            $msg = ConfigModel::setVal($name, '支付宝支付配置', input('post.config'));
            return json($msg);
        } else {
            $config = ConfigModel::getVal($name);
            if (! $config) {
                $config = [
                    'app_id'               => '',
                    'merchant_private_key' => '',
                    'notify_url'           => '',
                    'return_url'           => '',
                    'charset'              => 'UTF-8',
                    'sign_type'            => 'RSA2',
                    'gatewayUrl'           => 'https://openapi.alipay.com/gateway.do',
                    'alipay_public_key'    => '',
                    'MaxQueryRetry'        => '10',
                    'QueryDuration'        => '3',
                    'open'                 => 0,
                ];
            }
            View::assign('config', $config);
            return View::fetch();
        }
    }

    public function wechat()
    {
        $name = 'app_pay_wechat';
        if ($this->request->isPost()) {
            $fileSize = 1*1024*1024;
            $fileExt  = 'pem';
            $input = input();
            $apiclient_cert = $this->request->file('apiclient_cert');
            $apiclient_key  = $this->request->file('apiclient_key');
            if (!empty($apiclient_cert)) {
                try {
                    validate([ 'file' => ['fileExt' => $fileExt, 'fileSize' => $fileSize]])->check(['file' => $apiclient_cert]);
                    Filesystem::disk('root')->putFileAs('/', $apiclient_cert, 'apiclient_cert.pem');
                } catch ( ValidateException $e ) {
                    return json($e->getError());
                }
            }
            if (!empty($apiclient_key)) {
                try {
                    validate([ 'file' => ['fileExt' => $fileExt, 'fileSize' => $fileSize]])->check(['file' => $apiclient_key]);
                    Filesystem::disk('root')->putFileAs('/', $apiclient_key, 'apiclient_key.pem');
                } catch ( ValidateException $e ) {
                    return json($e->getError());
                }
            }
            $array = [];
            $array['app_id']     = $input['app_id'];
            $array['mch_id']     = $input['mch_id'];
            $array['key']        = $input['key'];
            $array['app_secret'] = $input['app_secret'];
            $array['open']       = (int) $input['open'];
            $msg = ConfigModel::setVal($name, '微信支付配置', $array);
            return json($msg);
        } else {
            $config = ConfigModel::getVal($name);
            if (! $config) {
                $config = [
                    'app_id'     => '',
                    'mch_id'     => '',
                    'key'        => '',
                    'app_secret' => '',
                    'open'       => 0,
                ];
            }
            $config['apiclient_cert'] = is_file(root_path() . 'apiclient_cert.pem');
            $config['apiclient_key']  = is_file(root_path() . 'apiclient_key.pem');
            View::assign('config', $config);
            return View::fetch();
        }
    }
}